unit UserModule;

interface

uses
  System.SysUtils, System.Classes, System.JSON,
  Sparkle.HttpServer.Context,
  EnhancedServerModule, UnifiedResponse;

type
  TUserModule = class(TEnhancedServerModule)
  public
    constructor Create(const ABaseUri: string); override;

  
    [TRoute('GET', '')]        //   /api/user
    [TRoute('GET', '/')]       //   /api/user/
    procedure GetUsers(Context: THttpServerContext);

    [TRoute('GET', '/{id}')]
    procedure GetUserById(Context: THttpServerContext);

    [TRoute('POST', '')]       //   /api/user
    [TRoute('POST', '/')]      //   /api/user/
    procedure CreateUser(Context: THttpServerContext);

    [TRoute('PUT', '/{id}')]
    procedure UpdateUser(Context: THttpServerContext);

    [TRoute('DELETE', '/{id}')]
    procedure DeleteUser(Context: THttpServerContext);

    [TRoute('GET', '/search')]
    procedure SearchUsers(Context: THttpServerContext);
  end;

implementation

{ TUserModule }

constructor TUserModule.Create(const ABaseUri: string);
begin
  inherited Create(ABaseUri);
  LogInfo('User module initialized with improved routing');
end;

procedure TUserModule.GetUsers(Context: THttpServerContext);
var
  UsersJSON: TJSONArray;
  UserObj: TJSONObject;
begin
  LogInfoContext(Context, 'Fetching all users');

  UsersJSON := TJSONArray.Create;
  try
    // ʾ������
    UserObj := TJSONObject.Create;
    UserObj.AddPair('id', '1');
    UserObj.AddPair('name', 'John Doe');
    UserObj.AddPair('email', 'john@example.com');
    UsersJSON.AddElement(UserObj);

    UserObj := TJSONObject.Create;
    UserObj.AddPair('id', '2');
    UserObj.AddPair('name', 'Jane Smith');
    UserObj.AddPair('email', 'jane@example.com');
    UsersJSON.AddElement(UserObj);

    TApiResponseHelper.SendSuccess(Context, UsersJSON.ToJSON, 'Users retrieved successfully');
  finally
    UsersJSON.Free;
  end;
end;

procedure TUserModule.GetUserById(Context: THttpServerContext);
var
  UserJSON: TJSONObject;
  UserId: string;
begin
 
  UserId := '1';  

  LogInfoContext(Context, Format('Fetching user by ID: %s', [UserId]));

  UserJSON := TJSONObject.Create;
  try
    UserJSON.AddPair('id', UserId);
    UserJSON.AddPair('name', 'John Doe');
    UserJSON.AddPair('email', 'john@example.com');
    UserJSON.AddPair('createdAt', FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now));

    TApiResponseHelper.SendSuccess(Context, UserJSON.ToJSON, 'User retrieved successfully');
  finally
    UserJSON.Free;
  end;
end;

procedure TUserModule.CreateUser(Context: THttpServerContext);
var
  JSONRequest, NewUserJSON: TJSONObject;
  UserName, Email: string;
begin
  LogInfoContext(Context, 'Creating new user');

  JSONRequest := TJSONObject.ParseJSONValue(
    TEncoding.UTF8.GetString(Context.Request.Content)) as TJSONObject;

  if JSONRequest = nil then
  begin
    TApiResponseHelper.SendError(Context, 'Invalid request format', 2003, 400);
    Exit;
  end;

  try
    UserName := JSONRequest.GetValue('name').Value;
    Email := JSONRequest.GetValue('email').Value;

    if (UserName = '') or (Email = '') then
    begin
      TApiResponseHelper.SendError(Context, 'Name and email are required', 2004, 400);
      Exit;
    end;

    NewUserJSON := TJSONObject.Create;
    try
      NewUserJSON.AddPair('id', TGUID.NewGuid.ToString);
      NewUserJSON.AddPair('name', UserName);
      NewUserJSON.AddPair('email', Email);
      NewUserJSON.AddPair('createdAt', FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now));

      Context.Response.StatusCode := 201;
      TApiResponseHelper.SendSuccess(Context, NewUserJSON.ToJSON, 'User created successfully');
    finally
      NewUserJSON.Free;
    end;
  finally
    JSONRequest.Free;
  end;
end;

procedure TUserModule.UpdateUser(Context: THttpServerContext);
var
  UpdateUserJSON: TJSONObject;
begin
  LogInfoContext(Context, 'Updating user');

  UpdateUserJSON := TJSONObject.Create;
  try
    UpdateUserJSON.AddPair('id', '1');
    UpdateUserJSON.AddPair('name', 'Updated Name');
    UpdateUserJSON.AddPair('email', 'updated@example.com');
    UpdateUserJSON.AddPair('updatedAt', FormatDateTime('yyyy-mm-dd"T"hh:nn:ss.zzz"Z"', Now));

    TApiResponseHelper.SendSuccess(Context, UpdateUserJSON.ToJSON, 'User updated successfully');
  finally
    UpdateUserJSON.Free;
  end;
end;

procedure TUserModule.DeleteUser(Context: THttpServerContext);
begin
  LogInfoContext(Context, 'Deleting user');
  TApiResponseHelper.SendSuccess(Context, '', 'User deleted successfully');
end;

procedure TUserModule.SearchUsers(Context: THttpServerContext);
var
  SearchResults: TJSONArray;
  UserObj: TJSONObject;
  Query: string;
begin
 // Query := Context.Request.Uri.Params.Get('q');
 Query:='测试';
  LogInfoContext(Context, Format('Searching users with query: %s', [Query]));

  SearchResults := TJSONArray.Create;
  try
    UserObj := TJSONObject.Create;
    UserObj.AddPair('id', '1');
    UserObj.AddPair('name', 'John Doe');
    UserObj.AddPair('email', 'john@example.com');
    UserObj.AddPair('matchScore', '0.95');
    SearchResults.AddElement(UserObj);

    TApiResponseHelper.SendSuccess(Context, SearchResults.ToJSON,
      Format('Search completed, found %d users', [SearchResults.Count]));
  finally
    SearchResults.Free;
  end;
end;

end.